package com.itheima.health.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.mapper.OrderSettingMapper;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.utils.date.DateUtils;
import org.apache.dubbo.config.annotation.Service;

import java.util.*;

@Service
@Transactional
public class OrderSettingServiceImpl extends ServiceImpl<OrderSettingMapper, OrderSetting> implements OrderSettingService {
    @Override
    public void batchOrderSetting(List<String[]> list) {

        //  解析excel 将数据 转换成Java 对象
        List<OrderSetting> orderSettings = StringTOList(list);
        List<OrderSetting> orderSettingSave = new ArrayList<OrderSetting>();//   存放没有id 的预约对象
        List<OrderSetting> orderSettingUpdate = new ArrayList<OrderSetting>();//   存放有id 的预约对象
        //  循环遍历集合  查询数据库表中有无 当前日期记录
        for (OrderSetting orderSetting : orderSettings) {
            Date orderDate = orderSetting.getOrderDate();
            QueryWrapper<OrderSetting> queryWrapper = new QueryWrapper<OrderSetting>();
            queryWrapper.eq("ORDERDATE", orderDate);
            OrderSetting orderSettingIsExist = baseMapper.selectOne(queryWrapper);//  根据条件查询单一记录对象
            if (orderSettingIsExist != null) {
                //  批量修改  需要将id  封装到List集合中
                orderSetting.setId(orderSettingIsExist.getId());//     查询id  封装到 添加OrderSetting 对象
                orderSettingUpdate.add(orderSetting);
            } else {
                orderSettingSave.add(orderSetting);
            }
        }
        saveBatch(orderSettingSave);
        updateBatchById(orderSettingUpdate);
    }

    /**
     * excel读取的数据  转换 List<OrderSetting> 集合对象
     *
     * @param list
     * @return
     */
    private List<OrderSetting> StringTOList(List<String[]> list) {
        if (list != null && list.size() != 0) {
            List<OrderSetting> orderSettingsList = new ArrayList<OrderSetting>();
            for (String[] strings : list) {
                OrderSetting orderSetting = new OrderSetting();
                orderSetting.setNumber(Integer.parseInt(strings[1]));
                Date date = DateUtils.parseString2Date(strings[0], "yyyy/MM/dd");
                orderSetting.setOrderDate(date);
                orderSetting.setReservations(0);
                orderSettingsList.add(orderSetting);
            }
            return orderSettingsList;
        } else {
            return null;
        }
    }

    //查询日历数据
    @Override
    public Map findSettingData(String year, String month) {
        //传入的年和月  不管是几月  直接查询31天的数据  如果数据库为空 就查询不到 也不会封装到返回数据中
        //起始日期和结束日期(如果是个位数的月份 name前台传过来的数据也是个位数)
        String beginDate = year + "-" + month + "-1";
        String endDate = year + "-" + month + "-31";

        //用SQL语句直接查询这个范围的数据
        List<Map> mapList = baseMapper.findOrderSettingsData(beginDate, endDate);

        //创建封装数据的map
        HashMap<String, OrderSetting> map = new HashMap<String, OrderSetting>();

        //key:日期(字符串)  value:数据(OrderSetting)
        for (Map map1 : mapList) {
            OrderSetting orderSetting = new OrderSetting();

            Integer number = (Integer) map1.get("number");  //预约上限
            Integer reservations = (Integer) map1.get("reservations");  //预约人数

            Date orderdate = (Date) map1.get("orderdate");  //预约日期

            orderSetting.setNumber(number);
            orderSetting.setReservations(reservations);
            map.put(DateUtils.parseDate2String(orderdate, "yyyy-MM-dd"), orderSetting);
        }
        return map;
    }


    //修改日期预约人数
    @Override
    public void updataNumByDay(String dateparam, int num) {

        QueryWrapper<OrderSetting> orderSettingQueryWrapper = new QueryWrapper<OrderSetting>();
        //查询总条数
        int x = baseMapper.selectCount(orderSettingQueryWrapper);
        System.out.println(x + "总条数");
        //判断数据库中该日期是否存在
        Object byDay = baseMapper.findByDay(dateparam);
        //查询不到说明没有 需要添加  id为总条数+1
        if (byDay == null) {
            //向数据库添加数据
            Date date = DateUtils.parseString2Date(dateparam, "yyyy-MM-dd");
            OrderSetting orderSetting = new OrderSetting();
            orderSetting.setId(x + 1);
            orderSetting.setOrderDate(date);
            orderSetting.setNumber(num);
            orderSetting.setReservations(0);
            baseMapper.insert(orderSetting);
            return;
        } else {
            //修改
            baseMapper.updataNumByDay(dateparam, num);
        }


    }


    //查询当天是否可以预约
    @Override
    public int isOrderSetting(String orderDate) {
        int count = baseMapper.isOrderSetting(orderDate);
        return count;
    }

    @Override
    public void upDateReservationsByOrderFate(String date) {
        baseMapper.upDateReservationsByOrderFate(date);
    }


}